const router = require('koa-router')();
const listFinancialEvents = require('../../app/mws/finances/ListFinancialEvents');
const listFinancialEventGroups = require('../../app/mws/finances/ListFinancialEventGroups');

router.prefix('/api/v1/finances');
router.get('/', function (ctx, next) {
  ctx.body = 'this is a listFinances response!'
});

router.get('/listing', async (ctx, next) => {
  const sellerId = ctx.headers.sellerid;
  const mwsAuthToken = ctx.headers.mwsauthtoken;

  try {
    let json = await listFinancialEvents.listFinancialEvents(sellerId, mwsAuthToken);
    ctx.response.status = 200;
    ctx.response.body = {
      data: json,
    };
  } catch (error) {
    ctx.response.status = 500;
    ctx.response.body = {
      data: error,
    };
  }
});

router.get('/listing/groups', async (ctx, next) => {
  const sellerId = ctx.headers.sellerid;
  const mwsAuthToken = ctx.headers.mwsauthtoken;

  try {
    let json = await listFinancialEventGroups.listFinancialEventGroups(sellerId, mwsAuthToken, new Date(2000, 1, 1));
    ctx.response.status = 200;
    ctx.response.body = {
      data: json,
    };
  } catch (error) {
    ctx.response.status = 500;
    ctx.response.body = {
      data: error,
    };
  }
});

module.exports = router;